<?php

namespace app\admin\controller\jdy;

use app\common\controller\Backend;
use think\Session;

use app\admin\library\Auth;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

use think\Log;


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
//use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;


use fast\Http;

/**
 * 公告栏留言管理
 *
 * @icon fa fa-circle-o
 */
class Bulletin extends Backend
{
    
    /**
     * Bulletin模型对象
     * @var \app\admin\model\jdy\Bulletin
     */
    protected $model = null;
    protected $isadmin = false;//

    const ANCHANG_ID = "__bulletinanchangid__";

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\jdy\Bulletin;

        $group_ids = $this->auth->getGroupIds();
        if (in_array("2", $group_ids) || in_array("1", $group_ids)) {
            $this->isadmin = true;//管理员权限不考虑管辖区域
        }

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        $anchang_id = 0;
        $url = $_SERVER['HTTP_REFERER'];
        $anchangname = "";
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $anchang_id = $urls[$size - 1];
            Session::set(self::ANCHANG_ID, $anchang_id);

            $anchang = \app\admin\model\jdy\Anchang::get($anchang_id);
            if ($anchang) {
                $anchangname = $anchang->name;

                $this->assignconfig('doedit', true);
            }
        } else {
            Session::set(self::ANCHANG_ID, 0);
        }

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($anchang_id > 0) {
                $total = $this->model
                    ->with(['anchang','task'])
                    ->where($where)
                    ->where('anchang_id', $anchang_id)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['anchang','task'])
                    ->where($where)
                    ->where('anchang_id', $anchang_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            } else {

                $anchangids = $this->getanchangids();
                $whereA = "";
                if (!$this->isadmin) {
                    if (empty($anchangids)) {
                        $whereA = " anchang_id  = 0 ";
                    } else {
                        $whereA = " anchang_id in (" . $anchangids . ") ";
                    }
                }
                $total = $this->model
                    ->with(['anchang','task'])
                    ->where($where)
                    ->where($whereA)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['anchang','task'])
                    ->where($where)
                    ->where($whereA)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            }

            foreach ($list as $row) {


            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        $this->view->assign("anchangname", $anchangname);
        return $this->view->fetch();
    }

    public function getanchanglist()
    {
        $anchangidlist = json_decode($this->auth->anchangids, TRUE);
        $xianshi = [];
        $xianshi[] = $this->auth->xianshi1;
        $xianshi[] = $this->auth->xianshi2;
        $xianshi[] = $this->auth->xianshi3;
        $xianshi[] = $this->auth->xianshi4;
        $xianshi[] = $this->auth->xianshi5;

        $xzq = [];
        $xzq[] = $this->auth->xzq1;
        $xzq[] = $this->auth->xzq2;
        $xzq[] = $this->auth->xzq3;
        $xzq[] = $this->auth->xzq4;
        $xzq[] = $this->auth->xzq5;

        $xianshis = "";//只有市
        $xianshisA = "";
        for ($i = 0; $i < 5; $i++) {
            if (!empty($xianshi[$i]) && empty($xzq[$i])) {
                if (empty($xianshis)) {
                    $xianshis = $xianshi[$i];
                    $xianshisA = "'" . $xianshis . "'";
                } else if (strpos($xianshis, $xianshi[$i]) === false) {
                    $xianshis .= ',' . $xianshi[$i];
                    $xianshisA .= ",'" . $xianshi[$i] . "'";
                }
            }
        }

        $xzqs = "";//
        $xzqsA = "";//
        for ($i = 0; $i < 5; $i++) {
            if (!empty($xzq[$i]) && !empty($xianshi[$i]) && strpos($xianshis, $xianshi[$i]) === false) {
                if (empty($xzqs)) {
                    $xzqs = $xzq[$i];
                    $xzqsA = "'" . $xzqs . "'";
                } else if (strpos($xzqs, $xzq[$i]) === false) {
                    $xzqs .= ',' . $xzq[$i];
                    $xzqsA .= ",'" . $xzq[$i] . "'";
                }
            }
        }

        $whereA = "";
        if (!empty($xianshis)) {
            $whereA = " xianshi in (" . $xianshisA . ") ";
        }

        if (!empty($xzqs)) {
            if (!empty($whereA)) {
                $whereA .= " or ";
            }
            $whereA .= " xzq in (" . $xzqsA . ") ";
        }

        $anchangids = "";//"117,116"
        if ($anchangidlist) {
            foreach ($anchangidlist as $row) {
                if (empty($anchangids)) {
                    $anchangids = $row['id'];
                } else {
                    $anchangids .= ',' . $row['id'];
                }
            }
        }

        if (!empty($anchangids)) {
            if (!empty($whereA)) {
                $whereA .= " or ";
            }
            $whereA .= " id in (" . $anchangids . ") ";
        }

        $list = [];
        if (!empty($whereA)) {
            $list = \app\admin\model\jdy\Anchang::where($whereA)->select();
        }

        return $list;
    }

    public function getanchangids() {
        $anchangidlist = $this->getanchanglist();
        $anchangids = "";
        foreach ($anchangidlist as $row) {
            if (empty($anchangids)) {
                $anchangids = $row['id'];
            } else {
                $anchangids .= ',' . $row['id'];
            }
        }
        return $anchangids;
    }
}
